package com.leaves.common.publicmethod.methods;

import cn.hutool.core.collection.CollectionUtil;
import com.leaves.entity.AuthInfo;
import com.leaves.entity.RoleInfo;
import com.leaves.entity.UserInfo;
import com.leaves.entity.vo.AuthInfoVo;
import com.leaves.entity.vo.RoleInfoVo;
import com.leaves.entity.vo.UserInfoVo;
import com.leaves.common.util.beanUtill.BeanUtils;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: LEAVES
 * @Version 1.0
 * @Date: 2021年08月23日 15时08分44秒
 * @Description: 用户处理类型
 */
@Component
public class UserInfoDeal {

    @Resource
    private MapperUtil mapperUtil;

    /**
     * 获取用户信息
     *
     * @param userId
     * @return
     */
    public UserInfoVo getUserInfo(String userId) {
        UserInfo userInfo = mapperUtil.userInfoMapper.selectById(userId);
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyEntity(userInfo, userInfoVo.getClass());
        List<String> roleIdList = getRoleIdListByUserId(userInfo.getId());
        List<RoleInfoVo> roleInfoList = getUserRoleInfo(roleIdList);
        userInfoVo.setRoleInfoList(roleInfoList);
        return userInfoVo;
    }

    /**
     * 获取用户信息
     *
     * @param userInfo
     * @return
     */
    public UserInfoVo getUserInfo(UserInfo userInfo) {
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyEntity(userInfo, userInfoVo.getClass());
        List<String> roleIdList = getRoleIdListByUserId(userInfo.getId());
        List<RoleInfoVo> roleInfoList = getUserRoleInfo(roleIdList);
        userInfoVo.setRoleInfoList(roleInfoList);
        return userInfoVo;
    }

    /**
     * 获取用户信息
     *
     * @param userInfoVo
     */
    public void getUserInfo(UserInfoVo userInfoVo) {
        List<String> roleIdList = getRoleIdListByUserId(userInfoVo.getId());
        List<RoleInfoVo> roleInfoList = getUserRoleInfo(roleIdList);
        userInfoVo.setRoleInfoList(roleInfoList);
    }

    /**
     * 获取用户角色列表信息
     *
     * @param roleIdList
     * @return
     */
    public List<RoleInfoVo> getUserRoleInfo(List<String> roleIdList) {
        List<RoleInfoVo> roleInfoVoList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(roleIdList)) {
            List<RoleInfo> roleInfoList = mapperUtil.roleInfoMapper.selectBatchIds(roleIdList);
            BeanUtils.copyList(roleInfoList,roleInfoVoList.getClass());
            roleInfoVoList.forEach(roleInfoVo -> {
                List<String> authIdList = getAuthIdListByRoleId(roleInfoVo.getId());
                List<AuthInfoVo> authInfoList = getUserAuthInfo(authIdList);
                roleInfoVo.setAuthInfoList(authInfoList);
            });
        }
        return roleInfoVoList;
    }

    /**
     * 获取用户权限列表信息
     *
     * @param authIdList
     * @return
     */
    public List<AuthInfoVo> getUserAuthInfo(List<String> authIdList) {
        List<AuthInfoVo> authInfoVoList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(authIdList)) {
            List<AuthInfo> authInfoList = mapperUtil.authInfoMapper.selectBatchIds(authIdList);
            BeanUtils.copyList(authInfoList, authInfoVoList.getClass());
        }
        return authInfoVoList;
    }

    /**
     * 根据用户ID查询角色ID列表
     *
     * @param userId
     * @return
     */
    public List<String> getRoleIdListByUserId(String userId) {
        return mapperUtil.userRoleMapper.selectRoleIdByUserId(userId);
    }

    /**
     * 根据角色ID查询权限ID列表
     *
     * @param roleId
     * @return
     */
    public List<String> getAuthIdListByRoleId(String roleId) {
        return mapperUtil.roleAuthMapper.selectAuthIdByRoleId(roleId);
    }

    /**
     * 修改用户状态
     *
     * @param userId 用户ID
     * @param online 用户状态
     */
    public boolean updateUserOnline(String userId, Integer online) {
        UserInfo userInfo = new UserInfo();
        userInfo.setId(userId);
        userInfo.setOnLine(online);
        return mapperUtil.userInfoService.updateById(userInfo);

    }

    /**
     * 修改用户状态
     *
     * @param userIdList 用户ID列表
     * @param online     用户状态
     */
    public boolean updateUserOnline(List<String> userIdList, Integer online) {
        List<UserInfo> userInfoList = new ArrayList<>();
        userIdList.forEach(userId -> {
            UserInfo userInfo = new UserInfo();
            userInfo.setId(userId);
            userInfo.setOnLine(online);
            userInfoList.add(userInfo);
        });
        return mapperUtil.userInfoService.updateBatchById(userInfoList);
    }
}
